// components/school/school.js
const {
  schoolAdd,
} = require('../../api/api')
const {
  voluntaryTemplateAdd
} = require('../../api/ai')
const {
  throttle
} = require('../../utils/util')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: []
    },
    mode: {
      type: String,
      value: 'school'
    },
    setting: {
      type: Object,
      value: {
        width: '750rpx'
      }
    },
    props: {
      type: Object,
      value: {
        name: 'name'
      }
    },
    rowGap: {
      type: String,
      value: ''
    },
    len: {
      type: Number,
      value: 0
    },
    maxLength: {
      type: Number,
      value: 6
    }
  },
  observers: {},
  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    goto(e) {
      const {
        mode
      } = this.data
      // if (mode !== 'school') return
      const {
        url,
      } = e.currentTarget.dataset
      wx.navigateTo({
        url
      })
    },
    //院校对比  院校选科
    compare(e) {
      const {
        list,
        len,
        maxLength
      } = this.data
      const {
        value,
        index,
        id,
        parent
      } = e.currentTarget.dataset
      if (len >= maxLength) {
        wx.showToast({
          title: '最多可选6个学校',
          icon: 'none'
        })
        return
      }
      const flag = list[parent][index].isCompare
      list[parent][index].isCompare = !flag
      if (flag) {
        // 删除
        this.triggerEvent('delete', {
          id,
          list
        })
      } else {
        this.triggerEvent('add', {
          value,
          list
        })
      }
      this.setData({
        list
      })
    },
    // async voluntaryAdd(e) {
     
    // },
    navigate(result) {
      const params = result
      params.level1Name = result.useBatch == '本科批' ? '本科' : '专科（高职）'
      params.schoolName=this.data.schoolName
      wx.navigateTo({
        url: '/pages/volunteer/select/index?params=' + encodeURIComponent(JSON.stringify(params))
      })

    },
    //添加志愿表
      volunteer:throttle(async function(e) {
      const {name}=e.currentTarget.dataset
      this.setData({
        schoolName:name
      })
      const {
        firstSelectSubjects,
        score,
        ranking,
        province,
        provinceId,
        secondSelectSubjects
      } = wx.getStorageSync('subjectInfo')
      let params = {
        firstSelect: firstSelectSubjects,
        province,
        provinceId,
        score,
        secondSelect: secondSelectSubjects,
        ranking,
      }
      const {
        result
      } = await voluntaryTemplateAdd(params)
      this.navigate(result)
      // const {
      //   list,
      //   len,
      //   maxLength
      // } = this.data
      // const {
      //   value,
      //   index,
      //   id,
      //   parent
      // } = e.currentTarget.dataset
      // if (len >= maxLength) {
      //   wx.showToast({
      //     title: '最多可选6个学校',
      //     icon: 'none'
      //   })
      //   return
      // }
      // const flag = list[parent][index].isAddVolunteer
      // list[parent][index].isAddVolunteer = !flag
      // if (flag) {
      //   // 删除
      //   this.triggerEvent('delete', {
      //     id,
      //     list
      //   })
      // } else {
      //   this.triggerEvent('add', {
      //     value,
      //     list
      //   })
      // }
      // this.setData({
      //   list
      // })
    }),
    // 院校收藏
    async collect(e) {
      const {
        id,
        index,
        pindex,
        iscollect
      } = e.currentTarget.dataset

      const {
        list
      } = this.data
      list[pindex][index].isCollection = !iscollect
      const {
        result
      } = await schoolAdd(id)
      if (iscollect) {
        wx.showToast({
          title: '取消收藏',
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '收藏成功',
          icon: 'none'
        })
      }
      const params = {
        list: this.data.list,
        pindex,
        index
      }
      this.triggerEvent("collect", params);
    }
  }
})